package com.shop.milk_tea_shop.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.shop.milk_tea_shop.dto.OrderOptions;

import java.util.*;

/**
 * @author 俞静雯
 * @Description TODO
 * @date 2024-06-23  23:07
 */
public class ItemUtils {
    /**
     * 解析附加信息string
     * @param addition
     * @return key:categoryId, value:addIdList
     */
    public static Map<Long, List<Long>> parseAddition(String addition) {
        HashMap<String, List<Long>> addMap = JSON.parseObject(addition, HashMap.class);
        Set<String> categoryId = addMap.keySet();
        Map<Long, List<Long>> result = new HashMap<>();
        categoryId.forEach(id -> result.put(Long.parseLong(id.substring(1,6)), addMap.get(id)));
        return result;
    }

    public static List<Long> getAddIdList(Set<Long> categoryIds, Map<Long, List<Long>> additionMap) {
        List<Long> addIdList = new ArrayList<>();
        categoryIds.forEach(id -> addIdList.addAll(additionMap.get(id)));
        return addIdList;
    }

    public static List<OrderOptions> parseOptions(String items){
        return JSON.parseObject(items, new TypeReference<List<OrderOptions>>(){});
    }
}
